package switchviz.core.alias;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import switchviz.core.ResourceManager;
import switchviz.gui.component.IListener;
import switchviz.gui.component.JLabeldComponent;
import switchviz.gui.component.PaddingPanel;

public class AliasConfigurationPanel extends PaddingPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7834784645444888562L;
	private AliasDataModel _aliasmodel;
	private JLabeldComponent _txtalias;
	private JLabeldComponent _txtnamepath;
	private JLabeldComponent _txtoidpath;
	private JLabeldComponent _txtdescr;
	private JButton _buttapply;

	public AliasConfigurationPanel()
	{
		initialize();
	}
	
	public void addListener(IListener l)
	{
		 _buttapply.addActionListener(l);
	}
	
	public void setValues(AliasDataModel aliasmodel)
	{
		_aliasmodel = aliasmodel;
		_txtalias.setText(_aliasmodel.getAlias());
		_txtnamepath.setText(_aliasmodel.getNamePath());
		_txtoidpath.setText(_aliasmodel.getOIDPath());
		_txtdescr.setText(_aliasmodel.getDescription());
		_txtdescr.getTextArea().setCaretPosition(0);
		_txtoidpath.getTextField().setToolTipText(_txtoidpath.getText());
		_txtnamepath.getTextField().setToolTipText(_txtnamepath.getText());
	}
	
	public AliasDataModel getValues()
	{
		_aliasmodel.setAlias(_txtalias.getText());
		_aliasmodel.setNamePath(_txtnamepath.getText());
		_aliasmodel.setOIDPath(_txtoidpath.getText());
		_aliasmodel.setDescription(_txtdescr.getText());
		return _aliasmodel;
	}
	
	private void initialize()
	{
		_txtalias = new JLabeldComponent("Alias: ", JLabeldComponent.TEXTFIELD);
		_txtalias.getTextField().setColumns(40);
		_txtnamepath = new JLabeldComponent("Name Path: ", JLabeldComponent.TEXTFIELD);
		_txtnamepath.getTextField().setColumns(40);
		_txtoidpath = new JLabeldComponent("OID Path: ", JLabeldComponent.TEXTFIELD);
		_txtoidpath.getTextField().setColumns(40); 
		_txtdescr = new JLabeldComponent("Description: ", JLabeldComponent.TEXTAREA);
		_buttapply = new JButton("Apply Changes");
		_buttapply.setActionCommand(ResourceManager.getInstance().getActionApply());
		JScrollPane scrollpane = new JScrollPane(_txtdescr);
        scrollpane.setMaximumSize(new Dimension(400, 200));
        JPanel buttonpanel = new JPanel();
        buttonpanel.setLayout(new BoxLayout(buttonpanel, BoxLayout.LINE_AXIS));
        buttonpanel.add(Box.createHorizontalGlue());
        buttonpanel.add(_buttapply);
        
        PaddingPanel confpane = new PaddingPanel();
        confpane.setLayout(new BoxLayout(confpane, BoxLayout.PAGE_AXIS));
        confpane.setBorder(BorderFactory.createLoweredBevelBorder());
        confpane.add(_txtalias);
        confpane.add(_txtnamepath);
        confpane.add(_txtoidpath);
        confpane.add(_txtdescr);
        confpane.add(new JLabel(" "));
        
        this.setLayout(ResourceManager.getInstance().getBorderLayout());
        this.add(confpane, BorderLayout.CENTER);
        this.add(buttonpanel, BorderLayout.SOUTH);
	}
}
